home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / viewers / polyview / polyvw31.lha / Polyview3.1 / new / placecam.c < prev    next >
C/C++ Source or Header  |  1993-08-09  |  2KB  |  78 lines

  1. /* $Header: /usr3/people/gbourhis/pv3/new/RCS/placecam.c,v 1.1 92/09/18 10:55:26 marca Exp $ */
  2.  
  3. #ifdef RCSLOG
  4. $Log:    placecam.c,v $
  5.  * Revision 1.1  92/09/18  10:55:26  marca
  6.  * Initial revision
  7.  * 
  8. #endif
  9.  
  10.  
  11. /* Listing 8.2   A simple camera specification */
  12.  
  13. #include <ri.h> 
  14.  
  15. /* 
  16.  * PlaceCamera(): establish a viewpoint, viewing direction and orientation
  17.  *    for a scene. This routine must be called before RiWorldBegin(). 
  18.  *    position: a point giving the camera position
  19.  *    direction: a point giving the camera direction relative to position
  20.  *    roll: an optional rotation of the camera about its direction axis 
  21.  */
  22.  
  23. PlaceCamera(position, direction, roll)
  24. RtPoint position, direction;
  25. float roll;
  26. {
  27.     RiIdentity();                 /* Initialize the camera transformation */
  28.     RiRotate(-roll, 0.0, 0.0, 1.0);
  29.     AimZ(direction);
  30.     RiTranslate(-position[0], -position[1], -position[2]);
  31. }
  32.  
  33. /* 
  34.  * AimZ(): rotate the world so the directionvector points in 
  35.  *    positive z by rotating about the y axis, then x. The cosine 
  36.  *    of each rotation is given by components of the normalized 
  37.  *    direction vector. Before the y rotation the direction vector 
  38.  *    might be in negative z, but not afterward.
  39.  */
  40. #define PI 3.14159265359
  41. #include <math.h>
  42. AimZ(direction)
  43. RtPoint direction;
  44. {
  45.     double xzlen, yzlen, yrot, xrot;
  46.  
  47.     if (direction[0]==0 && direction[1]==0 && direction[2]==0)
  48.         return;
  49.     /*
  50.      * The initial rotation about the y axis is given by the projection of
  51.      * the direction vector onto the x,z plane: the x and z components
  52.      * of the direction. 
  53.      */
  54.     xzlen = sqrt(direction[0]*direction[0]+direction[2]*direction[2]);
  55.     if (xzlen == 0)
  56.         yrot = (direction[1] < 0) ? 180 : 0;
  57.     else
  58.         yrot = 180*acos(direction[2]/xzlen)/PI;
  59.  
  60.     /*
  61.      * The second rotation, about the x axis, is given by the projection on
  62.      * the y,z plane of the y-rotated direction vector: the original y
  63.      * component, and the rotated x,z vector from above. 
  64.     */
  65.     yzlen = sqrt(direction[1]*direction[1]+xzlen*xzlen);
  66.     xrot = 180*acos(xzlen/yzlen)/PI;       /* yzlen should never be 0 */
  67.  
  68.     if (direction[1] > 0)
  69.         RiRotate(xrot, 1.0, 0.0, 0.0);
  70.     else
  71.         RiRotate(-xrot, 1.0, 0.0, 0.0);
  72.     /* The last rotation declared gets performed first */
  73.     if (direction[0] > 0)
  74.         RiRotate(-yrot, 0.0, 1.0, 0.0);
  75.     else
  76.         RiRotate(yrot, 0.0, 1.0, 0.0);
  77. }
  78.